package com.yungu.swift.admin.route.order;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.common.service.CommonSmsService;
import com.yungu.swift.order.model.dto.OrderAlarmDto;
import com.yungu.swift.order.model.dto.OrderComplainDto;
import com.yungu.swift.order.model.param.AdminOrderAlarmPageParam;
import com.yungu.swift.order.model.param.AdminOrderAlarmParam;
import com.yungu.swift.order.service.OrderAlarmService;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author : cuixiuyin
 * @date : 2019/7/19
 */
@RestController
@RequestMapping("/admin/v1/order/alarm")
@Api(value = "【订单管理】", tags = "报警信息管理")
public class OrderAlarmAdmin {

    @Reference
    private OrderAlarmService orderAlarmService;
    @Reference
    private CommonSmsService commonSmsService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "订单报警列表", httpMethod = "POST")
    public ResponseData<PageVo<OrderAlarmDto>> queryPage(@RequestBody AdminOrderAlarmPageParam alarmPageParam) {
        return orderAlarmService.queryPage(alarmPageParam);
    }


    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "订单报警详情", httpMethod = "GET")
    public ResponseData<OrderAlarmDto> detail(@RequestParam String uuid) throws Exception {
        return orderAlarmService.get(uuid);
    }

    @RequestMapping(value = "/handle", method = RequestMethod.POST)
    @ApiOperation(value = "订单报警处理", httpMethod = "POST")
    public ResponseData<Boolean> handle(@RequestBody AdminOrderAlarmParam alarmParam) {
        if (ParamUtil.isIllegal(alarmParam.getUuid()) || OrderComplainDto.STATUS_PENDING == alarmParam.getStatus()) {
            AdminErrorEnum paramError = AdminErrorEnum.PARAM_ERROR;
            return ResponseData.buildErrorResponse(paramError.getCode(), paramError.getMsg());
        }

        OrderAlarmDto orderAlarmDto = new OrderAlarmDto();
        BeanUtils.copyProperties(alarmParam, orderAlarmDto);
        orderAlarmDto.setUpdateBy(alarmParam.getUserUuid());
        orderAlarmDto.setUpdateOn(new Date());
        orderAlarmService.edit(orderAlarmDto);
        String msgContent = "乘客您好！平台已经处理您的报警信息，并将立即安排专员联系您，如您当前正在遭遇紧急情况不能接听电话，请直接拒接，我们的专员将立刻启动紧急预案联系警方介入处理。";

        return commonSmsService.sendCommon(alarmParam.getPassengerMobile(), msgContent, alarmParam.getAppid());
    }
}
